package com.trt.eatmeat.socket;
import com.trt.eatmeat.util.JsonUtils;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;


public class MyDecoder extends SimpleChannelInboundHandler<ByteBuf> {


    @Override
    protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {

            // 进入到这个channelRead0方法就表明之前的LengthFieldBasedFrameDecoder解码器已经把有效body数据解码出来了, 这个channelRead0方法得到的就是body有效数据.
        byte[] bytes = new byte[msg.readableBytes()];
        msg.readBytes(bytes);
        String json = new String(bytes, "UTF-8");
        MsgData msgData  = JsonUtils.JSONToObj(json,MsgData.class);
        ctx.fireChannelRead(msgData);
    }
}
